﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Localization;
using SinBiota.Model.DataTypes;
using SinBiota.Model.Entities.CustomAttributes;
//using SinBiota.Model.Localization.Resources;

namespace SinBiota.Model.Entities
{
    [LocalizedName("TaxonomicClassification", NameResourceType = typeof(Strings))]
    public class TaxonomicClassification : BaseEntity
    {
        public virtual String EOLTaxonId  { get; set; }
        
        // Taxonomia interna: COL 2011, update 11/2011
        public virtual Taxon Taxon { get; set; }

        // Sinbiota V1:
        [LocalizedName("LegacyTaxonomicGroup", NameResourceType = typeof(Strings))]
        public virtual LegacyTaxonomicGroup LegacyTaxonomicGroup { get; set; }

        [LocalizedName("LegacyTaxonomicClassification", NameResourceType = typeof(Strings))]
        public virtual String LegacyTaxonomicClassification { get; set; }


        public virtual IEnumerable<string[]>  LegacyTaxonomyPairs()
        {
            return LegacyTaxonomicClassification.Split(';').Select(pair => pair.Split('@'));
        }
        public virtual String LegacyTaxonomyHumanReadable()
        {
            var sb = new StringBuilder();
            foreach (var pair in LegacyTaxonomyPairs())
            {
                sb.AppendFormat("{0}:{1}; ", pair[0].ToUpper(), pair[1]);
            }
            return sb.ToString();
        }
        public virtual String LegacyTaxonomyRequiredRanksHumanReadable()
        {
            var groupRanks = LegacyTaxonomicGroup.RequiredRanks.Replace("_", string.Empty).Split(';');
            var sb = new StringBuilder();
            foreach (var pair in LegacyTaxonomyPairs().Where(pair => groupRanks.Contains(pair[0])))
            {
                sb.AppendFormat("{0}:{1}; ", pair[0].ToUpper(), pair[1]);
            }
            return sb.ToString();
        }
        public virtual IDictionary<string, string> LegacyTaxonomyToDictionary()
        {
            return LegacyTaxonomyPairs().ToDictionary(x => x[0], x => x[1]);
        }

        public virtual IDictionary<string, string> LegacyTaxonomyRequiredRanksToDictionary()
        {
            var groupRanks = LegacyTaxonomicGroup.RequiredRanks.Replace("_", string.Empty).Split(';');
            return LegacyTaxonomyPairs().Where(pair => groupRanks.Contains(pair[0])).ToDictionary(x => x[0], x => x[1]);
        }

        public virtual IDictionary<string, string> LegacyTaxonomyLocalizedToDictionary()
        {
            return LegacyTaxonomyPairs().ToDictionary(x => Taxonomy.ResourceManager.GetString(x[0]), x => x[1]);
        }
        public virtual object LegacyTaxonomyLocalizedToListPairs()
        {

            return LegacyTaxonomyPairs().Select(x => new { rank = Taxonomy.ResourceManager.GetString(x[0]), name = x[1] }).ToList();
        }

        public virtual object LegacyTaxonomyLocalizedRequiredRanksToListPairs()
        {
            var groupRanks = LegacyTaxonomicGroup.RequiredRanks.Replace("_",string.Empty).Split(';');
            return LegacyTaxonomyPairs().Where(pair => groupRanks.Contains(pair[0])).Select(x => new { rank = Taxonomy.ResourceManager.GetString(x[0]), name = x[1] }).ToList();
        }

        public virtual string LegacyScientificName()
        {
            var sb = new StringBuilder();
            var dict = LegacyTaxonomyToDictionary();
            if (dict.ContainsKey(LegacyTaxonClasses.Genus))
            {
                var a = dict[LegacyTaxonClasses.Genus];
                sb.AppendFormat("{0} ", char.ToUpper(a[0]) + a.Substring(1));
                foreach (var tax in new[] { LegacyTaxonClasses.Species, LegacyTaxonClasses.SubSpecies, LegacyTaxonClasses.InfraSpecies }.Where(dict.ContainsKey))
                {
                    sb.AppendFormat("{0} ", dict[tax]);
                }
                if (sb.Length > 0) return sb.ToString();
            }
            var deepest = new[] {LegacyTaxonClasses.Family, LegacyTaxonClasses.Order, LegacyTaxonClasses.Class,
                LegacyTaxonClasses.Phylum, LegacyTaxonClasses.Kingdom, LegacyTaxonClasses.Domain}.Where(dict.ContainsKey).First();
            return deepest + ": " + dict[deepest];
        }
    }


    public enum TaxonomyService
    {
        EOL = 1, ITIS = 2, CatalogueOfLife = 3,
    }

}
